<?php

namespace app\api\Service;

use think\Db;
use think\Config;
use app\common\library\Auth;
use addons\epay\library\Service;

class PollingService
{
    // 过期优惠劵
    public static function expireCoupon()
    {
        try{

            $batchSize = 1000; // 每次批量更新的数量
            Db::name('coupon_user')
                ->where(['status'=>1,'expiretime'=>['<',time()]])
                ->chunk($batchSize, function($data) {
                    foreach ($data as $row) {
                        // 进行更新操作
                        Db::name('coupon_user')->where('id', $row['id'])->update(['status'=>3,
                            'updatetime'=>time()]);

                    }
                });
            Db::commit();
            return ['code'=>1,'message'=>'操作成功','data'=>''];
        }catch(\Exception $e){
            Db::rollback();
            return ['code'=>0,'message'=>$e->getMessage(),'data'=>''];
        }
    }

    // 过期订单
    public static function expireOrder()
    {
        try{

            $batchSize = 1000; // 每次批量更新的数量
            Db::name('order')
                ->where(['paid'=>0,'status'=>0,'expiretime'=>['<',time()]])
                ->chunk($batchSize, function($data) {

                    foreach ($data as $row) {
                        // 进行更新操作
                        Db::name('order')->where('id', $row['id'])->update(['status'=>100,
                            'updatetime'=>time(),'canceltime'=>time()]);

                    }
                });
            Db::commit();
            return ['code'=>1,'message'=>'操作成功','data'=>''];
        }catch(\Exception $e){
            Db::rollback();
            return ['code'=>0,'message'=>$e->getMessage(),'data'=>''];
        }
    }
}